package com.huiquan.homoionym.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.homoionym.service.HomoionymSearchTermsService;
import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;

@Controller
public class HomoionymSearchTermsController {

	@Autowired
	private HomoionymSearchTermsService homoionymSearchTermsService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/homoionym/search_terms_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");
			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		String keyword = req.getParameter("keyword");
		String exactSearchFlag = req.getParameter("exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return homoionymSearchTermsService.list(start, keyword, exactSearchFlag,countPerPageStr);
	}

	@RequestMapping(value = "/homoionym/search_terms_pre_edit.htm")
	public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		String keyword = req.getParameter("keyword");
		String exactSearchFlag = req.getParameter("exactSearchFlag");
		String id = req.getParameter("id");
		User user = (User) req.getSession().getAttribute("user");

		return homoionymSearchTermsService.preEdit(keyword, exactSearchFlag, start, id, user);
	}

	@ResponseBody
	@RequestMapping(value = "/homoionym/search_terms_edit.do")
	public ReturnData edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		Long id = Long.parseLong(req.getParameter("id"));
		String[] words = req.getParameterValues("word");

		return homoionymSearchTermsService.edit(id, user, words);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/homoionym/search_terms_del.htm")
	public ModelAndView delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		Long id = Long.parseLong(req.getParameter("id"));
		String keyword = req.getParameter("keyword");
		String exactSearchFlag = req.getParameter("exactSearchFlag");
		User user = (User) req.getSession().getAttribute("user");
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return homoionymSearchTermsService.delete(start, id, keyword, exactSearchFlag, user,countPerPageStr);
	}

	@ResponseBody
	@RequestMapping(value = "/homoionym/search_terms_import.do")
	public ReturnData searchTermsImport(HttpServletRequest req, @RequestParam(required = true) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		return homoionymSearchTermsService.searchTermsImport(file, user);
	}

	@ResponseBody
	@RequestMapping(value = "/homoionym/search_terms_export.do")
	public void searchTermsExport(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		homoionymSearchTermsService.searchTermsExport(resp);
	}

	@ResponseBody
	@RequestMapping(value = "/homoionym/search_terms_checked_export.do")
	public void searchTermsCheckedExport(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		homoionymSearchTermsService.searchTermsCheckedExport(resp);
	}

	@ResponseBody
	@RequestMapping(value = "/homoionym/search_terms_expansion_data.do")
	public ReturnData searchTermsExpansionData(HttpServletRequest req, @RequestParam(required = true) MultipartFile dataFile)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		return homoionymSearchTermsService.searchTermsExpansionData(dataFile, user);
	}
}
